<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="功能室" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            label="功能室"
            label-width="fit-content"
            :prop="`${$index}.roomIds`"
            :rules="formRules.roomIds"
            class="mb-0px!"
          >
            <el-cascader
              class="w-full"
              v-model="row.roomIds"
              :options="roomList"
              :props="cascaderProps"
              clearable
              filterable
              placeholder="请选择功能室"
            />
            <!-- <el-select v-model="row.roomIds" placeholder="请选择功能室" multiple>
              <el-option
                v-for="dict in roomList"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              />
            </el-select> -->
          </el-form-item>
          <!-- <el-form-item :prop="`${$index}.roomIds`" :rules="formRules.roomIds" class="mb-0px!">
            <el-input v-model="row.roomIds" placeholder="请输入功能室Ids" />
          </el-form-item> -->
        </template>
      </el-table-column>
      <el-table-column label="班级" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            label="班级"
            label-width="fit-content"
            :prop="`${$index}.classIds`"
            :rules="formRules.classIds"
            class="mb-0px!"
          >
            <el-cascader
              class="w-full"
              v-model="row.classIds"
              :options="classList"
              :props="cascaderProps"
              clearable
              filterable
              placeholder="请选择班级"
            />
            <!-- <el-select v-model="row.classIds" placeholder="请选择班级" multiple>
              <el-option
                v-for="dict in classList"
                :key="dict.id"
                :label="dict.className"
                :value="dict.id"
              /> 
            </el-select>-->
          </el-form-item>
          <!-- <el-form-item :prop="`${$index}.classIds`" :rules="formRules.classIds" class="mb-0px!">
            <el-input v-model="row.classIds" placeholder="请输入班级IDs" />
          </el-form-item> -->
        </template>
      </el-table-column>

      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link>—</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button @click="handleAdd" round>+ 添加功能室排期</el-button>
  </el-row>
</template>
<script setup lang="ts">
import { RoomWeeksApi } from '@/api/school/room/weeks'
import { RoomApi } from '@/api/school/room'
import { ClassesApi } from '@/api/school/classes'
import { defaultProps } from '@/utils/tree'
const props = defineProps<{
  weekId: string // 周ID（主表的关联字段）
  roomType: string // 功能室类型
}>()
const formLoading = ref(false) // 表单的加载中
const formData: any = ref([])
const formRules = reactive({
  weekId: [{ required: true, message: '周ID不能为空', trigger: 'blur' }],
  roomIds: [{ required: true, message: '功能室不能为空', trigger: 'blur' }],
  classIds: [{ required: true, message: '班级不能为空', trigger: 'blur' }],
  sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
})
const formRef = ref() // 表单 Ref
const roomList = ref([]) //功能室列表
const classList = ref([]) //班级列表
const cascaderProps = computed(() => {
  return {
    ...defaultProps,
    multiple: true
  }
})
/** 新增按钮操作 */
const handleAdd = () => {
  const row: any = {
    id: undefined,
    weekId: props.weekId,
    roomIds: undefined,
    classIds: undefined,
    sort: undefined,
    status: undefined
  }
  formData.value.push(row)
}
/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.weekId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      handleAdd()
      return
    }
    try {
      formLoading.value = true
      formData.value = await RoomWeeksApi.getRoomScheduleListByWeekId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

const baseRoomList: any = ref([])
// const roomList = computed(() => {
//   console.log(props.roomType, baseRoomList.value, '---props.roomType')

//   return baseRoomList.value.filter((item) => item.roomType === props.roomType)
// })
const getRoomPage = async () => {
  try {
    const data = await RoomApi.getRoomPage({
      pageNo: 1,
      pageSize: 100
    })
    //查询功能室表
    roomList.value = await RoomApi.getRoomList(props.roomType)
    classList.value = await ClassesApi.getClassesList()
    baseRoomList.value = data.list
  } finally {
  }
}
// const classList: any = ref([])

const getClassList = async () => {
  try {
    classList.value = (await ClassesApi.getClassesSimpleList()) || []
  } catch (error) {
    console.log(error)
  } finally {
  }
}
/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}
onMounted(() => {
  getRoomPage()
  // getClassList()
})
defineExpose({ validate, getData })
</script>
